package com.hxh.basic.project.utils;

import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2UserGetRequest;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
public class DingdingUtil {

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private RedisTemplate redisTemplate;

    public static final String APPKEY = "dingzzaym6eavuh1cj8r";
    public static final String APPSECRET = "I1a2xDQSUvaCn8PBT4JlsLhc4mV8X49wTjGPYPuk4rKAxSSPWXFWlWlaFmOOzCLo";
    public static final String TOKEN_KEY = "unicom_club_token";

    /**
     * 获取token
     * @return
     */
    public  String getDingDingAccessToken(){
        if(redisTemplate.hasKey(TOKEN_KEY)){
            return redisTemplate.opsForValue().get(TOKEN_KEY).toString();
        }
        String url ="https://oapi.dingtalk.com/gettoken?appkey="+APPKEY+"&appsecret=" + APPSECRET;
        JSONObject jsonObject = restTemplate.getForObject(url,JSONObject.class);
        String accessToken = jsonObject.getString("access_token");
        redisTemplate.opsForValue().set(TOKEN_KEY,accessToken, 3600, TimeUnit.SECONDS);
        return accessToken;
    }

    /**
     * 根据token和授权码获取用户信息
     * @param code
     * @param token
     * @return
     */
    public JSONObject getUserInfoByAccessToken(String code,String token){
        String url = "https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token=" + token;
        HttpHeaders httpHeaders = setHeaders();
        Map<String,Object> map=new HashMap<>();
        map.put("code",code);
        HttpEntity<Map<String, Object>> entity = new HttpEntity<Map<String, Object>>(map, httpHeaders);
        JSONObject jsonObject = restTemplate.postForObject(url,entity,JSONObject.class);
        return jsonObject;
    }

    public HttpHeaders setHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Accept-Charset", "UTF-8");
        return headers;
    }

    public JSONObject getUserDetail(String dingdingUserId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
        OapiV2UserGetRequest req = new OapiV2UserGetRequest();
        req.setUserid(dingdingUserId);
        String token = this.getDingDingAccessToken();
        OapiV2UserGetResponse rsp = client.execute(req, token);
        JSONObject jsonObject=JSONObject.parseObject(rsp.getBody());
        JSONObject userInfoObject =jsonObject.getJSONObject("result");
        return userInfoObject;
    }

}
